import os,configparser,time
from baseframework.meslogger import MESLogger
from urllib import parse

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONFIG_DIR = os.path.join(BASE_DIR,r'config\config.ini')
LOG_DIR = os.path.join(BASE_DIR,r'logs')
TAG_DIR = os.path.join(BASE_DIR, r'tags')
logger = MESLogger(log_dir=LOG_DIR, name='mylog')
PREFIX = ''
try:
    config = configparser.ConfigParser()
    config.read(CONFIG_DIR,encoding='utf-8')

    WEATHER_TEMP = 'Weather_temp'
    PEOPLE_NO = 'People_No'

    REDIS_HOST = config['redis_server']['HOST']
    REDIS_PORT = int(config['redis_server']['PORT']) if config['redis_server'][
        'port'].isdigit() else 6379
    REDIS_PASSWORD = config['redis_server']['PASSWORD']

    USERNAME = config['DATABASE']['USERNAME']
    PASSWORD = parse.quote(config['DATABASE']['PASSWORD'])
    HOST = config['DATABASE']['HOST']
    PORT = config['DATABASE']['PORT']
    DB_NAME = config['DATABASE']['DB_NAME']
    DATABASE_SERVER_URI = f"mysql+mysqldb://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DB_NAME}?charset=utf8&autocommit=true"

    TAOS_SERVER_HOST = config['taos_server']['host']
    TAOS_SERVER_USER = config['taos_server']['user']
    TAOS_SERVER_PASSWORD = config['taos_server']['password']
    TAOS_SERVER_DATABASE = config['taos_server']['database']
    TAOS_ALARM_DATABASE = config['taos_server']['database_alarm']
    TAOS_SERVER_SECONDS = int(config['taos_server']['seconds']) if config['taos_server']['seconds'].isdigit() else 1

    SERVER_STATUS_TABLE= config['redis_runtime']['server_status_table']
    REDIS_STARITIME=config['redis_runtime']['start_time']
    REDIS_LASTREFRESHTIME=config['redis_runtime']['last_refresh_time']
    REDIS_SUCCESSCOUNT=config['redis_runtime']['is_success']
    REDIS_FAILCOUNT=config['redis_runtime']['is_failed']
    REDIS_REALSERVERNAME=config['redis_runtime']['real_server_name']
    REDIS_ALIVESATUS=config['redis_runtime']['alive_status']

    UNHANDLE = '未处理'
    TIME_OUT_ALARM = '超时报警'
    CHANGE_RATE_ALARM = '变化率报警'
    CONSIS_ALARM = '一致性报警'
    LOWER_ALARM = '超低限报警'
    LLOWER_ALARM = '超低低限报警'
    HIGHER_ALARM = '超高限报警'
    HHIGHER_ALARM = '超高高限报警'
    BIAS_ALARM = '偏差值报警'
    COUNT_ALARM = '计算值报警'
    # 超时时间定义
    OUT_TIME = '30m'
    # 变化率定义
    CHANGE_RATE = 0.5
    # 偏差率定义
    BIAS_RATE = 0.5

    EQUIP_ACCI = '设备故障'
    EQUIP_TABLE = 'data_equipment'
    ACCI_LOOP_TIME = 360

    MODEL_MODE_1 = '运算'
    RELATION_1 = 'AND'
    RELATION_2 = 'OR'
    RUN_LOOP_TIME = 36



except Exception as e:
    logger.error("读取配置文件出错！错误信息为："+str(e))
    time.sleep(30)